<?php
session_start();
require("../libs/Smarty.class.php");
require("../libs/myUtil.class.php");
include("../config/config.inc.php");
$smarty = new Smarty;

$id = $_POST["id"];
$nickname = $_POST["nickname"];
$content = $_POST["content"];
$category_id = $_POST["category_id"];
$url = $_POST["url"];
$tmpname = $_POST["tmpname"];

$error_flg = "n";
$error_photo_yn = "n";

//input.phpから来たかどうかを判断する処理
if($_SESSION["comment_edit_input_id"] != $_POST["comment_edit_input_id"]){
    header("Location:http://shonanbbs.com/admin/index.php");
}else{
    //同じページでの更新を防ぐ処理
    $_SESSION["comment_edit_input_id"] = rand();
}

//result.phpでconfirm.phpから来たかどうか判断するための処理
$_SESSION["comment_edit_confirm_id"] =rand();

//アップロードされたファイルがある場合(新規or更新）
if(is_uploaded_file($_FILES["photo"]["tmp_name"])){
    $ext = myUtil::getFileExt($_FILES["photo"]["tmp_name"]);
    if($ext == "."){
        $error_flg = "y";
        $error_photo_yn = "y";
    }else{
        session_regenerate_id();
        //myUtil::getFileExtは
        $tmpname ="photo_" .
                  session_id() .
                  myUtil::getFileExt($_FILES["photo"]["tmp_name"]);
        $smarty->assign("tmpname", $tmpname);
        copy($_FILES["photo"]["tmp_name"], "../tmp/".$tmpname);
        $_SESSION["tmpphoto"] = "../tmp/".$tmpname;
    }
//画像が存在しない、あるいは変更がない場合
}else{
        $smarty->assign("tmpname", $tmpname);
}

$smarty->assign("categorys",$categorys);//configからの配列もassgin

$smarty->assign("id", $id);
$smarty->assign("nickname",$nickname);
$smarty->assign("content",$content);
$smarty->assign("category_id",$category_id);
$smarty->assign("url",$url);
$_SESSION["tmpnickname"] = $nickname;
$_SESSION["tmpcontent"] = $content;
$_SESSION["tmpcategory_id"] = $category_id;
$_SESSION["tmpurl"] = $url;

$error_nickname_yn = "n";
$eroor_content_yn = "n";
if($nickname == ""){
    $error_flg ="y";
    $error_nickname_yn = "y";
}
if($content == ""){
    $error_flg ="y";
    $error_content_yn = "y";
}
if($error_flg == "y"){
    $error_url = "http://shonanbbs.com/admin/comment_edit_input.php?id={$id}";
    if($error_nickname_yn == "y"){
        $error_url .= "&error_nickname_yn=y";
    }
    if($error_content_yn == "y"){
        $error_url .= "&error_content_yn=y";
    }
    if($error_photo_yn == "y"){
    $error_url .= "&error_photo_yn=y";
    }

    header("Location:".$error_url);
}

$smarty->display("comment_edit_confirm.tpl");